home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MIDICraft's MIDINET CD-ROM
/
MIDICraft's MIDINET CD-ROM.iso
/
DOSUTILS
/
MIDIMAP.ZIP
/
MIDIMAP.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-03-11
|
4KB
|
186 lines
// midimap written by Günter Nagler 1996 (gnagler@ihm.tu-graz.ac.at)
#include "midiio.hpp"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static char* version = "midimap v1.0 by Günter Nagler (" __DATE__ ")";
#ifdef __MSDOS__
#define WRITE_BINARY "wb"
#define READ_BINARY "rb"
#else
#define WRITE_BINARY "w"
#define READ_BINARY "r"
#endif
char* input = 0;
char* output = 0;
MidiWrite* write = 0;
int quiet = 0;
int mapchannel[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
class MidiMap : public MidiCopy
{
public:
MidiMap(char* name);
virtual void error(const char* msg);
virtual void warning(const char* msg);
virtual void percent(int perc);
};
MidiMap::MidiMap(char* name) : MidiCopy(name)
{
}
void MidiMap::percent(int perc)
{
if (!quiet)
fprintf(stderr, "%-3d%%\r", perc);
}
void MidiMap::error(const char* msg)
{
printf("Error: %s\n", msg);
exit(1);
}
void MidiMap::warning(const char* msg)
{
printf("Warning: %s\n", msg);
}
void usage()
{
fprintf(stderr, "MidiMap maps channel numbers\n");
fprintf(stderr, "usage: MidiMap [-q][-map #[-#] #] input.mid output.mid\n");
fprintf(stderr, "-version\tget program version\n");
fprintf(stderr, "-q\tbe quiet\n");
fprintf(stderr, "-mapchannel c[-d] e\tmap events of channels c..d to channel e (1..16)\n");
exit(1);
}
int main(int argc, char**argv)
{
argc--; argv++;
while (argc > 0 && **argv == '-')
{
if (strncmp(*argv, "-version", 2) == 0)
{
fprintf(stderr, "%s\n", version);
argc--; argv++;
if (argc == 0)
return 0;
continue;
}
if (argc > 0 && strcmp(*argv, "-q") == 0)
{
quiet = 1;
argc--; argv++;
continue;
}
if (strncmp(*argv, "-mapchannel", 2) == 0)
{
int firstchannel, secondchannel, destchannel;
argc--; argv++;
if (argc > 0 && sscanf(*argv, "%u-%u", &firstchannel, &secondchannel) == 2)
{
if (firstchannel <= 0 || secondchannel > 16)
{
fprintf(stderr, "channel number %s out of range 1-16\n", *argv);
firstchannel = secondchannel = -1;
}
else if (firstchannel > secondchannel)
{
fprintf(stderr, "%s: first channel number must be less than second one\n", *argv);
firstchannel = secondchannel = -1;
}
argc--; argv++;
}
else if (argc > 0 && isdigit(**argv))
{
firstchannel = atoi(*argv);
if (firstchannel <= 0 || firstchannel > 16)
{
fprintf(stderr, "channel number %s out of range 1-16\n", *argv);
firstchannel = -1;
}
secondchannel = firstchannel;
argc--; argv++;
}
else
{
fprintf(stderr, "option -mapchannel expects two channel numbers 1-16\n");
usage();
break;
}
if (argc > 0 && isdigit(**argv))
{
destchannel = atoi(*argv);
argc--; argv++;
}
else
{
fprintf(stderr, "option -mapchannel expects two channel numbers 1-16\n");
usage();
break;
}
for (int c = firstchannel; c <= secondchannel; c++)
if (c != destchannel)
{
mapchannel[c-1] = destchannel-1;
}
continue;
}
fprintf(stderr, "invalid option %s\n", *argv);
argc--; argv++;
usage();
}
if (argc < 2)
usage();
input = argv[0];
output = argv[1];
if (strcmp(input, output) == 0)
{
fprintf(stderr, "cannot convert midi to same file\n");
return 1;
}
MidiMap midi(input);
if (!midi.getf())
{
perror(input);
return 1;
}
write = new MidiWrite(output);
if (!write)
{
fprintf(stderr, "out of memory\n");
return 1;
}
if (!write->getf())
{
perror(output);
return 1;
}
argc--; argv++;
for (int c = 0; c < 16; c++)
midi.mapchannel(c, mapchannel[c]);
midi.setoutput(write);
midi.options_ = OPTION_NOSYSEVENTS | OPTION_NOCONTROLS;
if (!midi.run())
fprintf(stderr, "%s: midi read error at %04lX\n", input, midi.getpos());
delete write;
return 0;
}